package com.knowledge.plus.正则;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ReUtil;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author TPF
 * @since 2024/12/19
 */
public class RegexDemo {

    @Test
    public void regex() {
        String url = "http://www.mysite.com/category/231";
        System.out.println(url.matches("^https?://.*$"));           // true
        System.out.println(Pattern.matches("^https?://.*$", url));  // true

        String str = "http://www.forta.com/\n" +
                "https://mail.forta.com/\n" +
                "ftp://ftp.forta.com/";
        String regex = ".+(?=:)";   // .+ 匹配任意文本，子表达式 (?=:) 匹配 :
        Pattern pattern = Pattern.compile(regex);   // 正则表达式的模式
        Matcher matcher = pattern.matcher(str);     // 匹配信息
        while (matcher.find()) {
            System.out.println(matcher.group());
            // 输出结果 http   https   ftp
        }
    }

    @Test
    public void hutool() {
        // ReUtil   正则工具
        String content = "ZZZaaabbbccc中文1234";
        boolean isMatch1 = ReUtil.isMatch("^[a-z]+\\d+", content);
        boolean isMatch2 = ReUtil.isMatch("^.+\\d+$", content);

        // IdcardUtil   身份证工具
        boolean isIdCard = IdcardUtil.isValidCard("321083197812162119");

        // Validator  字段验证器
        boolean isMatch = Validator.isMatchRegex("^.+\\d+$", "ZZZaaabbbccc中文1234");
    }

}
